<?php
/**
 * Modify by 			peace@warmyc.n
 * Creat Date			2009-03-11
 */
cache_delete_manage();
$cmd = isset($_GET['cmd'])? strtolower($_GET['cmd']) : 'list';
switch($cmd)
{
	case "add":
		Add();
		break;
	case "edit":
		Edit();
		break;
	case "delete":
		Del();
		break;
	case "save":
		Save();
		break;
	default:
		$cmd	= "list";
		View();
		break;
}
function View()
{
	set_cookie("SYSTEM_HTTP_REFERER", $_SERVER["REQUEST_URI"]);
	/**********************************************************************************************
	获取所有功能点
	**********************************************************************************************/
	$sql = "SELECT * FROM ".USERTABLE." ";
	$row = $GLOBALS['db']->query_row($sql,MYSQL_ASSOC);
?>
    <table Class="yc" cellspacing="1" cellpadding="0">  
	<tr>
	    <th width="50%">登录名</th>
		<th width="50%">真实姓名</th>
      </tr>
	  <?
		for($i=0;$i<count($row);$i++)
		{
			 print("<tr>");
			 print("<td>");
			 print("<a href=\"index.php?Model=".$GLOBALS['ATHENA_MODEL']."&cmd=edit&Id=".$row[$i]['id']."\">");
			 print($row[$i]['username']);
			 print("</a></td>");
			 print("<td>{$row[$i]["realname"]}</td>");
			 print("</tr>");
		}
	  ?>
    </table>
<br>

<?}

/**************************************************************************************************
编辑功能点表单

**************************************************************************************************/
function Edit()
{
	//获取修改记录ID
	$User			= $_GET['Id'];
	$sql = "SELECT u.id, u.name, ISNULL(g.user) ucheck FROM ". GROUPNAMETABLE ." u LEFT OUTER JOIN ". GROUPTABLE ." g ON u.id = g.`group`  and g.User='$User'";
	$row =$GLOBALS['db']->query_row($sql);
	$sql			= "select username from ".USERTABLE." where id='$User'";
	$name				= $GLOBALS['db']->query_value($sql,MYSQL_ASSOC);
?>
    <table Class="yc" cellspacing="1" cellpadding="0">   
	<tr><td colspan="2">管理员：<?=$name?></td></tr>
    <?
			for($i=0;$i<count($row);$i++)
	    {
		  print("<tr>");
		  print("<td width=\"10\" style=\"text-align:center\">");
		  if($row[$i]['ucheck']==0)
		    print("<input type=\"checkbox\" name=\"group[]\" value=\"".$row[$i]['id']."\" checked=\"true\"></td>");
		  else
			print("<input type=\"checkbox\" name=\"group[]\" value=\"".$row[$i]['id']."\"></td>");
		  print("<td width='100%'>".$row[$i]['name']."</td>");
		  print("</tr>");
	     }
	   ?>

	  <tr bgcolor="#FFFFFF">
		<td colspan="2" height="50" style="text-align:center">
			<input name="Submit" type="submit" class="button" value="保存" onclick="AppendCmd('save')"/>
	      	<input name="back" type="submit" class="button" value="返回" onclick="AppendCmd('back')"/>
	      	<input type="hidden" name="SYSTEM_HTTP_REFERER" value="<?=get_cookie("SYSTEM_HTTP_REFERER")?>">
			<input type="hidden" name="ID" value="<?=$User?>">
		</td></tr>
    </table>
<?	
}
function Save()
{
	Add_Op_Log("修改管理员隶属角色");
	//保存修改记录
	$checked = array();
	if(isset($_POST['group']))
		$checked=$_POST['group'];
	$user=$_POST['ID'];
	if(count($checked) == 0)
	{
		$sql = "delete from `".GROUPTABLE."` where User='$user'";	
		$GLOBALS['db']->query($sql);
	}
	else
	{
		$str = join(",",$checked);
		$sql = "delete from `".GROUPTABLE."` where User='$user' and `Group` not in($str)";
		$GLOBALS['db']->query($sql);
		$sql = "select `Group` from `".GROUPTABLE."` where User='$user'";
		while (list(,$value) = each($checked)) {
			$sql = "insert into `".GROUPTABLE."`(User,`Group`) select * from (select '$user' as User,'{$value}' as `Group` ) a where not exists(select 1 from `".GROUPTABLE."` b where a.User=b.User and a.Group=b.Group)"	;
			$GLOBALS['db']->query($sql);
		}
	}
	ANotify::halt("修改用户隶属组<font color=red>信息</font>成功！",'SUCCESS');
}
?>